import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '@views/Home'

Vue.use(VueRouter)

const routes = [{
        path: '/',

        redirect: '/home',
        meta: {
            islogin: false
        },
    },
    {
        path: '/home',
        name: 'Home',
        component: Home,
        meta: {
            islogin: false
        },
    },
    {
        path: '/center',
        name: 'Center',
        meta: {
            islogin: true
        },

        component: () =>
            import ( /* webpackChunkName: "about" */ '@views/Center')
    }, {
        path: '/cart',
        name: 'Cart',
        meta: {
            islogin: true
        },

        component: () =>
            import ( /* webpackChunkName: "about" */ '@views/Cart')
    }, {
        path: '/cate',
        name: 'Cate',
        meta: {
            islogin: false
        },

        component: () =>
            import ( /* webpackChunkName: "about" */ '@views/Cate')
    }, {
        path: '/category',
        name: 'CateGory',
        meta: {
            islogin: false
        },

        component: () =>
            import ( /* webpackChunkName: "about" */ '@views/CateGory')
    }, {
        path: '/search',
        name: 'Search',
        meta: {
            islogin: false
        },

        component: () =>
            import ( /* webpackChunkName: "about" */ '@views/Search')
    }, {
        path: '/login',
        name: 'Login',
        meta: {
            islogin: false
        },

        component: () =>
            import ( /* webpackChunkName: "about" */ '@views/Login')
    }, {
        path: '/adduser',
        name: 'Adduser',
        meta: {
            islogin: false
        },

        component: () =>
            import ( /* webpackChunkName: "about" */ '@views/Adduser')
    }, {
        path: '/placeorder',
        name: 'PlaceOrder',
        meta: {
            islogin: true
        },

        component: () =>
            import ( /* webpackChunkName: "about" */ '@views/PlaceOrder')
    }, {
        path: '/receivingaddress',
        name: 'ReceivingAddress',
        meta: {
            islogin: true
        },

        component: () =>
            import ( /* webpackChunkName: "about" */ '@views/ReceivingAddress')
    }, {
        path: '/add_address',
        name: '增加收货地址',
        meta: {
            islogin: true
        },

        component: () =>
            import ( /* webpackChunkName: "about" */ '@views/Add_address')
    }, {
        path: '/address_list',
        name: '收货地址列表',
        meta: {
            islogin: true
        },

        component: () =>
            import ( /* webpackChunkName: "about" */ '@views/Address_list')
    }, {
        path: '/searchpage',
        name: '搜索页面',
        meta: {
            islogin: false
        },

        component: () =>
            import ( /* webpackChunkName: "about" */ '@views/SearchPage')
    }, {
        path: '/searchdata',
        name: '搜索内容',
        meta: {
            islogin: false
        },

        component: () =>
            import ( /* webpackChunkName: "about" */ '@views/SearchData')
    }
]

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
    return originalPush.call(this, location).catch(err => err)
}

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})


router.beforeEach((to, from, next) => {
    //判断路由是否需要登录权限
    if (to.meta.islogin) {
        // 判断用户客户端是否有token存在
        if (localStorage.getItem('token')) {
            // 存在 ： 放行
            next()
        } else {
            // 不存在：重定向到登录页
            next({
                path: '/login',
                from: from.path
            })
        }
    } else {
        next()
    }

    next()
})

export default router